<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    title="重置密码"
    :close-func="closeFunc"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { editSchema } from './data';
  import { resetUserPwdApi } from '@/api/system/user';
  // import { getPostAllApi } from '@/api/system/post';
  // import { addWorkflowCategoryApi, updateWorkflowCategoryApi } from '@/api/workflow/category';
  // import type { WorkflowCategoryItem } from '@/api/workflow/model/categoryModel';

  const rowId = ref();
  const text = ref('');

  const emit = defineEmits(['success', 'register']);

  const [registerForm, { resetFields, validate }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: editSchema,
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    rowId.value = data.id;
    text.value = `请输入${data.username}的密码`;
  });

  const closeFunc = async () => {
    await resetFields();
    return true;
  };

  async function handleSubmit() {
    try {
      const { password } = await validate();
      setModalProps({ confirmLoading: true });
      // TODO custom api
      // console.log(rowId.value, values);
      const res = await resetUserPwdApi({ id: rowId.value, password: password });
      console.log(res);
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
